# == Schema Information
# Schema version: 20080914230643
#
# Table name: invitation_requests
#
#  id                       :integer(4)      not null, primary key
#  open_space_participation :boolean(1)
#  workshops_attendance     :boolean(1)
#  comment                  :string(255)
#  status                   :string(255)
#  status_changed_at        :datetime
#  status_reason            :text
#  person_id                :integer(4)
#  created_at               :datetime
#  updated_at               :datetime
#

class InvitationRequest < ActiveRecord::Base
	class Statuses
		VALUES = %w(created accepted declined)
		CREATED = VALUES[0]
		ACCEPTED = VALUES[1]
		DECLINED = VALUES[2]
	end

	belongs_to :person
	
	validates_presence_of :person, :message => 'must belong to a person'
	validates_inclusion_of :status, :in => Statuses::VALUES, :message => "invalid status"
	validates_presence_of :status_changed_at
	
	def change_status status_sym, reason=nil
		status_string = "Statuses::#{status_sym.to_s.upcase}"
		self.status = instance_eval(status_string)
		self.status_changed_at = DateTime.now
		self.status_reason = reason
	end
	
	def validate
		return if open_space_participation == true or workshops_attendance == true
		
    errors.add(:open_space_participation, "may be selected")
    errors.add(:workshops_attendance, "may be selected")
	end
	
	def self.for email_address
		person = Person.find_by_email_address email_address
		return nil if person.nil?
		
		invitation_request = InvitationRequest.find_by_person_id person.id
		invitation_request
	end
	
	def self.created
		InvitationRequest.find(:all, :conditions => { :status => InvitationRequest::Statuses::CREATED })
	end
end
